// pages/grapply/grapply.js
const util = require("../../utils/util")
Page({

  /**
   * 页面的初始数据
   */
  data: {
    upImg: '../../images/spxq_sclogo.png',
    fileKey:'',
    name:'',
    mobile:'',
    address:''
  },
  bindInput(e){
    this.setData({
      [e.currentTarget.dataset.id]:e.detail.value
    })
  },
  bindsave(){
    if(!this.data.fileKey || !this.data.name || !this.data.mobile || !this.data.address)
      {
        wx.showToast({
          title: '请输入完整',
          icon: 'warning',
          duration: 2000
        })
      }
    let that = this
    wx.showLoading({
      title: '加载中'
    })
    let param = {
      "dealerType": "individual_dealer",
      "address": that.data.address,
      "dealerName": that.data.name,
      "mobile": that.data.mobile,
      "visualizeFileKey":that.data.fileKey
    }
    util.post({
      url:"https://groomtea.icebartech.com/api/miniApp/salesman/addDealer",
      data:JSON.stringify(param),
      success:(res)=>{
        console.log(res) 
        wx.hideLoading({})
        wx.showToast({
          title: '保存成功',
          icon:'success',
          duration: 2000
        })
        wx.navigateBack({
          delta: 1
        })
      },
      fail:(error)=>{
        console.log(error)
      }
    })
  } ,
  upImg(){
    let that = this;
    wx.chooseImage({
      count: 1,
      success: function (res) {
        let tempFilePaths = res.tempFilePaths[0],
            tempFile = res.tempFiles[0];
        console.log('chooseImage-------',res);
        if(tempFile.size/1024/1024>2){
        wx.showToast({
          title:'大小不能超过2M'
        })
        return false;
      };
      that.upload(tempFilePaths,tempFile)
    }, 
          fail:(res)=>{
            console.log(res);
          }
        });
  },
  upload(path,file){
    let that = this;
    console.log('上传图片');
    let suffix = /\.[^\.]+$/.exec(path)[0].slice(1);
    let contentType = '';
    if(suffix == 'jpg' || suffix == 'jpeg'){
      contentType = 'image/' + 'jpeg'
    }else{
      contentType = 'image/' + suffix
    }
    console.log('去获取链接',suffix,contentType);
    let url = 'https://groomtea.icebartech.com/api/base/sys/mini/getOSSUploadFormData';
    let dataForm = {
      suffix:suffix,
      contentType:contentType
    }
    util.http({
      url: url,
      dataForm:dataForm,
      success: function (res) {
        console.log('获得上传链接',res.data.bussData.formData,path);
        let resData = res.data.bussData;
        let formData = Object.assign(resData.formData,{name:path})
        let uploadUrl = resData.uploadUrl;
        let fileKey = resData.fileKey;
        that.data.fileKey = fileKey
        console.log('formData----',formData);
        wx.uploadFile({
          url:uploadUrl,
          filePath:path,
          name:'file',
          formData:formData,
          success(res){
            const data = res.data;
            console.log('success--',res);
            util.post({
              url:'https://groomtea.icebartech.com/api/admin/base/getGenerateDownloadUrl',
              header: { 'content-type': 'application/x-www-form-urlencoded' },
              data:{
                fileKey:fileKey,
              },
              success:res=>{
                console.log(res);
                let img = res.data.bussData;
                that.setData({
                  upImg:img,
                  fileKey:fileKey
                })
              },
              fail:err=>{
                console.log(err);
              }
            })
          },
          fail:function(e){
            console.log('上传失败')
          }
        })
        if(res.data.status == 401){
          wx.showToast({
            title: '登录已过期,请重新登陆',
            icon: 'none'
          })
          wx.clearStorageSync();
          wx.navigateTo({
            url: '../login/login',
          })
          return;
        }
      },
      fail: err => {
        console.log(err);
        wx.hideToast();
        wx.showModal({
          title: '错误提示',
          content: '上传图片失败',
          showCancel: false,
          success: function (res) { }
        })
        fail && fail(err)
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})